package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.AlbumService;
import com.changgou.pojo.Album;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AlbumController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-08-02
 * @Version V1.0
 **/
@RestController
@RequestMapping("album")
public class AlbumController {
    @Autowired
    private AlbumService albumService;

    @GetMapping("findAll")
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询所有执行成功", albumService.findAll());
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable Long id) {
        return new Result(true, StatusCode.OK, "根据ID查询成功", albumService.findById(id));
    }

    @PostMapping("add")
    public Result add(@RequestBody Album album) {
        albumService.add(album);
        return new Result("添加图片成功", null);
    }

    @PutMapping("update/{id}")
    public Result update(@PathVariable Long id, @RequestBody Album album) {
        album.setId(id);
        albumService.update(album);
        return new Result("更新成功", null);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        albumService.delete(id);
        return new Result("删除成功", null);
    }

    @GetMapping("search/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                             @RequestParam Map<String, Object> searchMap) {
        Page<Album> page = albumService.search(pageNum, pageSize, searchMap);
        long total = page.getTotal();
        List<Album> result = page.getResult();
        PageResult pageResult = new PageResult(total, result);
        return new Result("分页查询成功", pageResult);

    }
}
